package library.database.user;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

//import cn.edu.sysu.ssejava.database.DatabaseProxy;

public class DatabaseAdminProxy extends DatabaseUserProxy{
	private Connection conn = getConnection();
	static DatabaseBookProxy bkproxy;
	public DatabaseAdminProxy() throws IOException, ClassNotFoundException, SQLException {
		super();
		bkproxy = new DatabaseBookProxy();
		conn.setAutoCommit(false);
	}
	/**
	 * 删除用户信息 
	 * @return int
	 * @throws SQLException 
	 */
	public int delete(String id) throws SQLException {
		try{
			String sql = "DELETE FROM user where id = ?";
			ps =conn.prepareStatement(sql);
			ps.setString(1,id);
			int res = ps.executeUpdate();
			conn.commit();
			return res;
		}catch(Exception e){
			conn.rollback();
			e.printStackTrace();
			return 0;
		}
	}
	/**
	 * 返回用户列表
	 * @return
	 * @throws SQLException
	 */
	public ResultSet showUsers() throws SQLException {
		try{
			String sql = "SELECT * FROM user;";
			ps =conn.prepareStatement(sql);
			ResultSet res = ps.executeQuery();
			conn.commit();
			return res;		
		}catch(Exception e){
			conn.rollback();
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 添加图书信息
	 * @param name
	 * @param author
	 * @param type
	 * @param price
	 * @return
	 */
	public boolean addBooks(String name,String author,String type,double price,int num){
		try {
			return bkproxy.addBook(name, author, type, price,num);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}		
	}
	public boolean deleteBook(int id) throws SQLException
	{
		bkproxy.deletebookfromusrbk(id);
		return bkproxy.deleteBook(id);
	}
	public boolean checkifex(int id) throws SQLException
	{
		return bkproxy.isExisted(id);
	}
	/**
	 * 返回图书库存信息
	 * @param bkid
	 * @return
	 * @throws SQLException
	 */
	public int checkbooknum(int bkid) throws SQLException
	{
		return bkproxy.CheckNum(bkid);
	}
	/**
	 * 修改图书信息
	 * @param fun
	 * @param info
	 * @param id
	 * @throws SQLException
	 */
	public void modifybook(int fun,String info,int id) throws SQLException{
		bkproxy.modify(fun,info,id);
	}
	/**
	 * 显示在借数量
	 * @param bkid
	 * @return
	 * @throws SQLException
	 */
	public int getBonum(int bkid) throws SQLException
	{
		int num = 0;
		ResultSet rs = bkproxy.getBorrowedBooks(bkid);
		while(rs.next()){
			num ++;
		}
		return num;
	}



}